using System.Diagnostics;

namespace FilesBrowserTool
{
    public partial class FileBrowserTool : Form
    {
        public FileBrowserTool()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PopulateTreeView();

            listView1.View = View.Details;
            listView1.Columns.Add("Name", 150);
            listView1.Columns.Add("Type", 100);
            listView1.Columns.Add("Last Modified", 150);

            // 这里设置每列的宽度，-2 表示自适应宽度
            foreach (ColumnHeader column in listView1.Columns)
            {
                column.Width = -2;
            }
        }

        private void PopulateTreeView()
        {
            TreeNode rootNode;

            DirectoryInfo info = new DirectoryInfo(@"C:\Users\" + Environment.UserName + @"\Documents");
            if (info.Exists)
            {
                rootNode = new TreeNode(info.Name);
                rootNode.Tag = info;
                GetDirectories(info.GetDirectories(), rootNode);
                treeView1.Nodes.Add(rootNode);
            }
        }

        private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
        {
            TreeNode aNode;
            DirectoryInfo[] subSubDirs;
            foreach (DirectoryInfo subDir in subDirs)
            {
                aNode = new TreeNode(subDir.Name, 0, 0);
                aNode.Tag = subDir;
                aNode.ImageKey = "folder";
                subSubDirs = subDir.GetDirectories();
                if (subSubDirs.Length != 0)
                {
                    GetDirectories(subSubDirs, aNode);
                }
                nodeToAddTo.Nodes.Add(aNode);
            }
        }

        private void treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)
        {
            DirectoryInfo nodeDirInfo = (DirectoryInfo)e.Node.Tag;
            ListViewItem.ListViewSubItem[] subItems;
            ListViewItem item = null;

            listView1.Items.Clear();

            foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
            {
                item = new ListViewItem(dir.Name, 0);
                subItems = new ListViewItem.ListViewSubItem[]
                          { new ListViewItem.ListViewSubItem(item, "Directory"),
                    new ListViewItem.ListViewSubItem(item, dir.LastAccessTime.ToShortDateString())};
                item.SubItems.AddRange(subItems);
                listView1.Items.Add(item);
            }

            foreach (FileInfo file in nodeDirInfo.GetFiles())
            {
                item = new ListViewItem(file.Name, 1);
                subItems = new ListViewItem.ListViewSubItem[]
                          { new ListViewItem.ListViewSubItem(item, "File"),
                    new ListViewItem.ListViewSubItem(item, file.LastAccessTime.ToShortDateString())};
                item.SubItems.AddRange(subItems);
                listView1.Items.Add(item);
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView1_DoubleClick_1(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                ListViewItem item = listView1.SelectedItems[0];
                string path = Path.Combine(((DirectoryInfo)treeView1.SelectedNode.Tag).FullName, item.Text);
                if (File.Exists(path) && path.EndsWith(".txt"))
                {
                    Process.Start("notepad.exe", path);
                }
            }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Simple File Explorer\nVersion 1.0", "About");
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 我的文档ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode rootNode;

            DirectoryInfo info = new DirectoryInfo(@"C:\Users\" + Environment.UserName + @"\Documents");
            if (info.Exists)
            {
                rootNode = new TreeNode(info.Name);
                rootNode.Tag = info;
                GetDirectories(info.GetDirectories(), rootNode);
                treeView1.Nodes.Clear();
                treeView1.Nodes.Add(rootNode);
            }
        }

        private void 我的视频ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode rootNode;

            // 获取我的视频文件夹的路径
            string videoFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
            DirectoryInfo info = new DirectoryInfo(videoFolderPath);

            if (info.Exists)
            {
                rootNode = new TreeNode(info.Name);
                rootNode.Tag = info;
                GetDirectories(info.GetDirectories(), rootNode);
                treeView1.Nodes.Clear();
                treeView1.Nodes.Add(rootNode);
                rootNode.Expand();  // 可以选择展开根节点
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            PopulateTreeView();
        }

        private void 我的桌面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode rootNode;

            // 获取我的视频文件夹的路径
            string videoFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            DirectoryInfo info = new DirectoryInfo(videoFolderPath);

            if (info.Exists)
            {
                rootNode = new TreeNode(info.Name);
                rootNode.Tag = info;
                GetDirectories(info.GetDirectories(), rootNode);
                treeView1.Nodes.Clear();
                treeView1.Nodes.Add(rootNode);
                rootNode.Expand();  // 可以选择展开根节点
            }
        }
    }
}
